package main

import (
	"context"
	"encoding/json"
	"fmt"
	"gin_study_blog/common"
	"gin_study_blog/core"
	"gin_study_blog/models"
	"gin_study_blog/utils"
	"github.com/olivere/elastic/v7"
	"github.com/sirupsen/logrus"
)

func main() {
	// 读取配置文件
	core.YamlInitConfig()
	// 连接 es
	common.EsClient = core.EsConnect()

	query := elastic.NewMatchAllQuery()
	result, _ := common.EsClient.
		Search(models.ArticleModel{}.Index()).
		Query(query).
		Do(context.Background())

	for _, hit := range result.Hits.Hits {
		var article models.ArticleModel
		_ = json.Unmarshal(hit.Source, &article)

		indexList := utils.GetSearchIndexDataByContent(hit.Id, article.Title, article.Content)

		// 创建一个桶
		bulk := common.EsClient.Bulk()
		for _, indexData := range indexList {
			req := elastic.NewBulkIndexRequest().Index(models.FullTextModel{}.Index()).Doc(indexData)
			bulk.Add(req)
		}
		res, err := bulk.Do(context.Background())
		if err != nil {
			logrus.Error(err)
			continue
		}
		fmt.Println(article.Title, "添加成功", "共", len(res.Succeeded()), "条")
	}

}
